cc.Class({
    extends: cc.Component,
    // 属性列表：定义属性绑定到编辑器
    properties: {
        // 该节点负责监听触摸事件（设置后绑定节点）
        stick: {
            default: null,
            type: cc.Node,
        },
        // 摇杆区域半径
        max_r : 120,
        min_r : 60,
    },

    // 初始化游戏
    onLoad () {
        // 初始化，游戏值设置
        this.dir = -1;
        this.radius = 0;
        // 摇杆控制，触摸移动事件、摇杆区域
        this.stick.on(cc.Node.EventType.TOUCH_MOVE, function (e) {
            let w_pos = e.getLocation();    // 获取触摸坐标
            let pos = this.node.convertToNodeSpaceAR(w_pos);     // 将一个点转换到节点 (局部) 空间坐标系
            //console.log(pos);
            // 设置摇杆区域
            // let len = cc.pLength(pos); // 旧版
            let len = pos.mag();
            if (len > this.max_r) {
                pos.x = pos.x * this.max_r / len;
                pos.y = pos.y * this.max_r / len;
            }
            //debugger
            // this.stick.setPosition(pos);
            this.stick.x = pos.x;
            this.stick.y = pos.y;
            // 获取摇杆角度，对外提供摇杆方向
            if (len < this.min_r) {
                return;
            }
            this.dir = -1;
            let r = Math.atan2(pos.y, pos.x);
            if (r >= -8 * Math.PI / 8 && r < -7 * Math.PI / 8) {
                this.dir = 2;
            }
            else if (r >= -7 * Math.PI / 8 && r < -5 * Math.PI / 8)
            {
                this.dir = 6;
            }
            else if (r >= -5 * Math.PI / 8 && r < -3 * Math.PI / 8)
            {
                this.dir = 1;
            }
            else if (r >= -3 * Math.PI / 8 && r < -1 * Math.PI / 8)
            {
                this.dir = 7;
            }
            else if (r >= -1 * Math.PI / 8 && r < Math.PI / 8)
            {
                this.dir = 3;
            }
            else if (r >= Math.PI / 8 && r < 3 * Math.PI / 8)
            {
                this.dir = 4;
            }
            else if (r >= 3 * Math.PI / 8 && r < 5 * Math.PI / 8)
            {
                this.dir = 0;
            }
            else if (r >= 5 * Math.PI / 8 && r < 7 * Math.PI / 8)
            {
                this.dir = 5;
            }
            else if (r >= 7 * Math.PI / 8 && r <= 8 * Math.PI / 8)
            {
                this.dir = 2;
            }
            this.radius = r;
            console.log(this.dir);
        }.bind(this), this);

        // 重置，监听触摸结束事件
        this.stick.on(cc.Node.EventType.TOUCH_END, function (e) {
            //this.stick.setPosition(cc.p(0, 0))  //cc.p is deprecated. Please use cc.v2 instead
            //this.stick.setPosition(cc.v2(0, 0))  //cc.p is deprecated. Please use cc.v2 instead
            this.stick.x = 0;
            this.stick.y = 0;
        }.bind(this), this);

        // 监听触摸取消事件
        this.stick.on(cc.Node.EventType.TOUCH_CANCEL, function (e) {
            //this.stick.setPosition(cc.p(0, 0))
            //this.stick.setPosition(cc.v2(0, 0))
            this.stick.x = 0;
            this.stick.y = 0;
        }.bind(this), this);
    },

    // 初始
    start () {

    },

    // 每次刷新时调用
    update (dt) {

    },
});
